Advanced Process Management
Process management is a critical aspect of Unix system administration, involving the creation, monitoring, and control of processes. This tutorial will explore advanced techniques in process management, including process creation, signals, job control, and performance monitoring.
Introduction to Process Management
A process is an instance of a running program. Unix systems use processes to manage the execution of programs and tasks. Advanced process management requires a deep understanding of process creation, signals, job control, and performance monitoring to ensure efficient and reliable system operation.
Process Creation
Processes can be created using various commands and system calls. Here are some common methods for process creation:
Fork and Exec
The fork
system call creates a new process by duplicating the calling process. The exec
family of functions replaces the current process image with a new process image:
pid_t pid = fork();
if (pid == 0) {
// Child process
execl("/bin/ls", "ls", NULL);
} else if (pid > 0) {
// Parent process
wait(NULL);
} else {
// Fork failed
perror("fork");
}
System Command
The system
function executes a shell command in a new process:
system("ls -l");
Shell Commands
Shell commands can be used to create processes directly from the command line:
$ ls -l
Signals
Signals are used to communicate with and control processes. Here are some common signals and their purposes:
- SIGHUP: Hangup detected on controlling terminal or death of controlling process.
- SIGINT: Interrupt from keyboard (Ctrl+C).
- SIGQUIT: Quit from keyboard (Ctrl+\).
- SIGKILL: Kill signal (cannot be caught or ignored).
- SIGTERM: Termination signal.
- SIGSTOP: Stop process (cannot be caught or ignored).
- SIGCONT: Continue if stopped.
Sending Signals
Use the kill
command to send signals to processes:
$ kill -SIGTERM pid
$ kill -9 pid # Send SIGKILL signal
Handling Signals
Processes can handle signals using signal handlers:
#include
#include
#include
void handle_signal(int signal) {
printf("Received signal %d\n", signal);
}
int main() {
signal(SIGINT, handle_signal);
while (1) {
printf("Running...\n");
sleep(1);
}
return 0;
}
Job Control
Job control allows you to manage multiple processes within a single shell session. Here are some common job control commands:
Background and Foreground
Use the &
operator to run a command in the background:
$ command &
Use the fg
command to bring a background job to the foreground:
$ fg %job_id
Job Listing
Use the jobs
command to list all jobs in the current shell session:
$ jobs
Stopping and Continuing Jobs
Use the Ctrl+Z
key combination to stop a foreground job:
$ command
^Z
[1]+ Stopped command
Use the bg
command to continue a stopped job in the background:
$ bg %job_id
Performance Monitoring
Performance monitoring is essential for ensuring efficient and reliable system operation. Here are some common tools for monitoring process performance:
top
The top
command provides a real-time view of system processes and resource usage:
$ top
ps
The ps
command displays information about active processes:
$ ps aux
htop
The htop
command is an interactive process viewer with a user-friendly interface:
$ htop
vmstat
The vmstat
command reports virtual memory statistics:
$ vmstat
iostat
The iostat
command reports CPU and I/O statistics:
$ iostat
netstat
The netstat
command displays network connections, routing tables, and interface statistics:
$ netstat
Example: Monitoring and Managing a Long-Running Process
Let's create a script to monitor and manage a long-running process. The script will start the process, monitor its resource usage, and send an alert if the process exceeds a specified resource threshold:
Monitoring Script
#!/bin/bash
# Function to send an alert
send_alert() {
echo "Resource usage exceeded threshold. Sending alert..."
# Add code to send an email or notification
}
# Start the long-running process in the background
long_running_process &
PID=$!
# Monitor the process
while true; do
# Get the CPU and memory usage of the process
CPU_USAGE=$(ps -p $PID -o %cpu=)
MEM_USAGE=$(ps -p $PID -o %mem=)
# Check if the process exceeds the resource threshold
if (( $(echo "$CPU_USAGE > 80.0" | bc -l) )) || (( $(echo "$MEM_USAGE > 80.0" | bc -l) )); then
send_alert
fi
# Sleep for a specified interval before checking again
sleep 60
done
Practice Exercises
Here are some practice exercises to help you develop your process management skills:
- Write a script to start multiple background processes and monitor their resource usage. Send an alert if any process exceeds a specified resource threshold.
- Write a script to periodically check the status of a critical process and restart it if it is not running.
- Write a script to log the resource usage of a process over time and generate a report.
- Write a script to manage a batch of jobs, ensuring that only a specified number of jobs run concurrently.
- Write a script to capture and handle specific signals (e.g., SIGINT, SIGTERM) and perform cleanup tasks before exiting.
Check out some Bands on Bandcamp.com. Altogether Steve and the Mercenaries, Crazy Fingers (Vancouver 1991), Flying Butt Pliers, and Hammy Ham Hands.
Proudly powered by a Text Editor, an Sftp client and some Internet Searches.
2025 dispelled.ca end of file.